var app = getApp();
Page({
    data: {
        phone: "",
        pwd: "",
        linktype: 1,
        url: ''
    },
    onLoad: function (options) {
        //登录成功后跳转类型(1,2,3) navgitorback , redirect ,switchTab
        this.setData({
            linktype: options.t || 3,
            url: options.url || '../home/index'
        })
    },
    textinput: function (event) {
      console.log(event)
        //判断是账号还是密码输入
        var type = event.currentTarget.dataset.type;
        if (type == 1) {
            this.setData({
                phone: event.detail.value
            })
        } else {
            this.setData({
                pwd: event.detail.value
            })
        }
    },
    login: function () {
        var that = this;
      var url = /^0\d{2,3}\d{7,8}$|^1[34578]\d{9}$/.test(that.data.phone) ? "/hystream/login" : "/hystream/login"
       wx.showLoading({
            title: '登录中...',
            icon: 'loading'
        })
      let data = {
        userName: that.data.phone,
        phone: that.data.phone,
        userPasswd: that.data.pwd
      }
      /**
       * 登陆
       */
      app.netRequest({
        url:'/hystream/login',
        data:data,
        success:function(res){
          if (null != res && res!=''&& res != 'undefined') {
          if (res.code != 0) {
            wx.hideLoading();
            wx.showModal({
              title: '提示',
              content: '登录失败，请重试！'
            })
            return;
          }
          console.log(res)
          //判断是养殖场管理员(1)还是政府检疫员(2)
          if ('养殖场管理员' === res.data.roleName) {
            wx.navigateTo({
              url: '../indexOne/indexOne?UserType=1'
            })
          } else {
            wx.navigateTo({
              url: '../indexTwo/indexTwo?UserType=2'
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '登录失败，请重试！'
          })
          return;
        }
      }
    })
  }
})